<!DOCTYPE html>
<!--
Copyright (c) 2015 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->

<link rel="import" href="/tracing/base/math/rect.html">
<link rel="import" href="/tracing/extras/chrome/cc/debug_colors.html">
<link rel="import" href="/tracing/extras/chrome/cc/util.html">
<link rel="import" href="/tracing/model/object_instance.html">

<script>
'use strict';

tr.exportTo('tr.e.cc', function() {
  const ObjectSnapshot = tr.model.ObjectSnapshot;

  /**
   * @constructor
   */
  function TileSnapshot() {
    ObjectSnapshot.apply(this, arguments);
  }

  TileSnapshot.prototype = {
    __proto__: ObjectSnapshot.prototype,

    preInitialize() {
      tr.e.cc.preInitializeObject(this);
    },

    initialize() {
      tr.e.cc.moveOptionalFieldsFromArgsToToplevel(
          this, ['layerId', 'contentsScale', 'contentRect']);
      if (this.args.managedState) {
        this.resolution = this.args.managedState.resolution;
        this.isSolidColor = this.args.managedState.isSolidColor;
        this.isUsingGpuMemory = this.args.managedState.isUsingGpuMemory;
        this.hasResource = this.args.managedState.hasResource;
        this.scheduledPriority = this.args.scheduledPriority;
        this.gpuMemoryUsageInBytes = this.args.gpuMemoryUsage;
      } else {
        this.resolution = this.args.resolution;
        this.isSolidColor = this.args.drawInfo.isSolidColor;
        this.isUsingGpuMemory = this.args.isUsingGpuMemory;
        this.hasResource = this.args.hasResource;
        this.scheduledPriority = this.args.scheduledPriority;
        this.gpuMemoryUsageInBytes = this.args.gpuMemoryUsage;
      }

      // This check is for backward compatability. It can probably
      // be removed once we're confident that most traces contain
      // content_rect.
      if (this.contentRect) {
        this.layerRect = this.contentRect.scale(1.0 / this.contentsScale);
      }

      if (this.isSolidColor) {
        this.type_ = tr.e.cc.tileTypes.solidColor;
      } else if (!this.hasResource) {
        this.type_ = tr.e.cc.tileTypes.missing;
      } else if (this.resolution === 'HIGH_RESOLUTION') {
        this.type_ = tr.e.cc.tileTypes.highRes;
      } else if (this.resolution === 'LOW_RESOLUTION') {
        this.type_ = tr.e.cc.tileTypes.lowRes;
      } else {
        this.type_ = tr.e.cc.tileTypes.unknown;
      }
    },

    getTypeForLayer(layer) {
      let type = this.type_;
      if (type === tr.e.cc.tileTypes.unknown) {
        if (this.contentsScale < layer.idealContentsScale) {
          type = tr.e.cc.tileTypes.extraLowRes;
        } else if (this.contentsScale > layer.idealContentsScale) {
          type = tr.e.cc.tileTypes.extraHighRes;
        }
      }
      return type;
    }
  };

  ObjectSnapshot.subTypes.register(TileSnapshot, {typeName: 'cc::Tile'});

  return {
    TileSnapshot,
  };
});
</script>
